﻿using System;
using System.Configuration;
using bba.framework.infraweb.baseinfra;
using bba.framework.infraweb.entities;
using bba.framework.infraweb.infrastructure.constants;
using bba.framework.infraweb.interfaces.controller;
using Microsoft.Practices.Unity;


namespace bba.framework.infraweb.masterpage.Pages
{
    public partial class Search : BasePage
    {
        #region Fields

        private SearchList list;
        private string qtdRegisterPage;
        private string searchKey;
        private string currentPage;

        #endregion

        #region Properties

        /// <summary>
        /// Propriedade responsável por definir os métodos e 
        /// eventos da página <seealso cref="bba.framework.infraweb.masterpage.Pages.Search"/>
        /// </summary>
        [Dependency]
        public ISearchPageController PageController { get; set; }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ViewState["currentPaging"] == null)
                ViewState["currentPaging"] = currentPage = "1";
            else
                currentPage = ViewState["currentPaging"].ToString();
            qtdRegisterPage = ConfigurationManager.AppSettings[Constant.QtdResultsSearch];

            searchKey = Request.QueryString[0];
            if (!Page.IsPostBack)
            {
                list = PageController.LoadInfo(list, int.Parse(currentPage), int.Parse(qtdRegisterPage), searchKey, resultRepeater, endResultLabel,
                    currentResultLabel, setaPagDir, setaPagEsq, setaStartEsq, setaEndDir);
                if (list.ListSearch.Count == 0)
                    NoResults();
            }

        }

        protected void setaPagEsq_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int current = Convert.ToInt32(ViewState["currentPaging"].ToString());
            current--;
            ViewState["currentPaging"] = current;
            PageController.LoadInfo(list, current, int.Parse(qtdRegisterPage), searchKey, resultRepeater, endResultLabel,
                    currentResultLabel, setaPagDir, setaPagEsq, setaStartEsq, setaEndDir);
        }

        protected void setaPagDir_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int current = Convert.ToInt32(ViewState["currentPaging"].ToString());
            if (current != null)
                current++;
            ViewState["currentPaging"] = current;
            PageController.LoadInfo(list, current, int.Parse(qtdRegisterPage), searchKey, resultRepeater, endResultLabel,
                currentResultLabel, setaPagDir, setaPagEsq, setaStartEsq, setaEndDir);
        }

        protected void setaEndEsq_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int fimPag = int.Parse(endResultLabel.Text);
            ViewState["currentPaging"] = fimPag;
            PageController.LoadInfo(list, fimPag, int.Parse(qtdRegisterPage), searchKey, resultRepeater, endResultLabel,
                currentResultLabel, setaPagDir, setaPagEsq, setaStartEsq, setaEndDir);
        }

        protected void setaStartEsq_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            ViewState["currentPaging"] = 1;
            PageController.LoadInfo(list, 1, int.Parse(qtdRegisterPage), searchKey, resultRepeater, endResultLabel,
                currentResultLabel, setaPagDir, setaPagEsq, setaStartEsq, setaEndDir);
        }



        #endregion

        #region Methods Private

        private void NoResults()
        {
            noResult.Visible = true;
            endResultLabel.Visible = false;
            currentResultLabel.Visible = false;
            infoResult.Visible = false;
            infoDeResult.Visible = false;
            setaStartEsq.Visible = false;
            setaEndDir.Visible = false;

        }

        #endregion



    }

}
